﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using NUnit.Framework;

namespace Comdiv.QWeb.Test.Renders
{
	[TestFixture(Description = "coverage-propose test of default viewengine/view render working")]
	public class base_view_render_test
	{
		[Test]
		public void somehow_it_works() {
			var sw = new StringWriter();
			var ctx = new QWebContext("http://localhost/qweb/echo.view.qweb"){Output = sw}.Set("x", "test_string");
			new QWebHandler().ProcessRequest(ctx);
			var html = sw.ToString();
			Assert.True(html.Contains("test_string"));
			Assert.True(html.Contains("<textarea"));
		}
	}

	public class error_throw_action:IAction {
		public object Process(QWebContext context) {
			throw new Exception("test_error");
		}
	}

	[TestFixture]
	public class error_rendering_test{
		void test(string type,string test ) {
			var sw = new StringWriter();
			var ctx = new QWebContext("http://localhost/error/throw." + type + ".qweb") {Output = sw};
			new QWebHandler().ProcessRequest(ctx);
			Console.WriteLine(sw.ToString());
			Assert.True(Regex.IsMatch(sw.ToString(),test));
		}

		[Test]
		public void string_error() {
			test("string",@"(?ix)^System.Exception:\stest_error");
		}

		[Test]
		public void js_error()
		{
			test("js", @"(?ix)^\{""type"":\s""Exception"",\s""message"":\s""test_error"",[\s\S]+?\}$");
		}
		[Test]
		public void json_error()
		{
			test("json", @"(?ix)^\{""type"":\s""Exception"",\s""message"":\s""test_error"",[\s\S]+?\}$");
		}

		[Test]
		public void xml_error()
		{
			test("xml", @"(?ix)^<error><!\[CDATA\[System.Exception:\stest_error");
		}

		[Test]
		public void view_error()
		{
			test("view", @"(?ix)^<div\sclass='error'><h2>error[\s\S]+?test_error");
		}


		[Test]
		[Ignore("Bxl is experimental and not much usable render")]
		public void bxl_error()
		{
			test("bxl", @"(?ix)^error:\s*""""""System.Exception:\stest_error[\s\S]+?""""""");
		}
	}
}
